/**
 * 
 */
package edu.pw.treegrid.server.query;

import java.util.HashSet;
import java.util.Set;
import edu.pw.treegrid.server.reportmodel.ReportColumn;

/** 
 * @author Piotrek
 */
public class TableNode extends Node {

	private String tableName;

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	private Integer number;

	public Integer getNumber() {
		return number;
	}

	public void setNumber(Integer number) {
		this.number = number;
	}

	private String primaryKeyName;

	public String getPrimaryKeyName() {
		return primaryKeyName;
	}

	public void setPrimaryKeyName(String primaryKeyName) {
		this.primaryKeyName = primaryKeyName;
	}

	private String foreignKeyName;

	public String getForeignKeyName() {
		return foreignKeyName;
	}

	public void setForeignKeyName(String foreignKeyName) {
		this.foreignKeyName = foreignKeyName;
	}

	private Set<Node> childs = new HashSet<Node>();

	public Set<Node> getChilds() {
		return childs;
	}

	public void setChilds(Set<Node> childs) {
		this.childs = childs;
	}

	public String getTableAlias() {
		return tableName + "_" + number;
	}

	public void addChild(Node node) {
		node.setParent(this);
		childs.add(node);
	}

	@Override
	public void generateQuery(Query query) {
		StringBuilder sb = new StringBuilder(query.getFrom());
		sb.append("\n\t");
		if (parent != null) {
			sb.append("left outer join ");
		}
		sb.append(getTableName());
		sb.append(" ");
		sb.append(getTableAlias());
		if (parent != null) {
			sb.append(" on ");
			sb.append(parent.getTableAlias());
			sb.append(".");
			sb.append(getForeignKeyName());
			sb.append(" = ");
			sb.append(getTableAlias());
			sb.append(".");
			sb.append(getPrimaryKeyName());
		}
		query.setFrom(sb.toString());
		for (Node n : getChilds()) {
			n.generateQuery(query);
		}
	}

	/** 
	 * /* (non-Javadoc)
	 *  * @see Node#matchesGroupColumn(ReportColumn column)
	 *
	 */
	public boolean matchesGroupColumn(ReportColumn column) {
		for (Node c : getChilds()) {
			if (!c.matchesGroupColumn(column)) {
				return false;
			}
		}
		return true;
	}

	/** 
	 * /* (non-Javadoc)
	 *  * @see Node#getLeafs()
	 */
	public Set<ColumnNode> getLeafs() {
		Set<ColumnNode> leafs = new HashSet<ColumnNode>();
		for (Node c : getChilds()) {
			leafs.addAll(c.getLeafs());
		}
		return leafs;
	}

	public void removeChild(Node c) {
		this.childs.remove(c);
		c.setParent(null);
	}

	@Override
	public boolean matches(Node node) {
		if (getClass() != node.getClass())
			return false;
		TableNode other = (TableNode) node;
		return this.getTableName().equals(other.getTableName()) &&
			   this.getForeignKeyName().equals(other.getForeignKeyName());
	}
/*
	@Override
	public void setReportColumn(ReportColumn reportColumn) {
		super.setReportColumn(reportColumn);
		
		for(Node n: childs) {
			n.setReportColumn(reportColumn);
		}
	}
*/
}